<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$fine              = 0;
$amount            = 0;
$bullionDetail     = array();
$total['kg']       = 0;
$total['cramount'] = 0;
$total['crfine']   = 0;
$total['dramount'] = 0;
$total['totalAmt'] = 0;

$selectbullionDetail = "SELECT bulliondetail.bulliondetailid,bullionmaster.bullionmasterid,bullionmaster.partyId,bulliondetail.bullionmasterid,DATE_FORMAT(bullionmaster.bulliondate,'%Y-%m-%d') AS bulliondate,bulliondetail.particulars,bulliondetail.kg,bulliondetail.bhav,bulliondetail.cramount,bulliondetail.crfine,bulliondetail.dramount
                          FROM bulliondetail
                          JOIN bullionmaster ON bulliondetail.bullionmasterid = bullionmaster.bullionmasterid
                         WHERE bullionmaster.partyId = ".$_POST['partyId']."
                           AND bullionmaster.bulliondate <= '".date("Y-m-d")."'
                         ORDER BY bullionmaster.bulliondate, bulliondetail.bullionmasterid";
$selectbullionDetailRes = mysql_query($selectbullionDetail);
$bullionDetailRow = mysql_num_rows($selectbullionDetailRes);
$b = 0;
while($bullionDetailRow = mysql_fetch_array($selectbullionDetailRes))
{
  $bullionDetail[$b]['bulliondetailid'] = $bullionDetailRow['bulliondetailid'];
  $bullionDetail[$b]['bullionmasterid'] = $bullionDetailRow['bullionmasterid'];
  $bullionDetail[$b]['bulliondate']     = $bullionDetailRow['bulliondate'];
  $bullionDetail[$b]['particulars']     = $bullionDetailRow['particulars'];
  $bullionDetail[$b]['kg']              = $bullionDetailRow['kg'];
  $bullionDetail[$b]['bhav']            = $bullionDetailRow['bhav'];
  $bullionDetail[$b]['cramount']        = $bullionDetailRow['cramount'];
  $bullionDetail[$b]['crfine']          = $bullionDetailRow['crfine'];
  $bullionDetail[$b]['dramount']        = $bullionDetailRow['dramount'];
  $b++;
  
  $total['kg']       += $bullionDetailRow['kg'];
  $total['cramount'] += $bullionDetailRow['cramount'];
  $total['crfine']   += $bullionDetailRow['crfine'];
  $total['dramount'] += $bullionDetailRow['dramount'];
  $total['totalAmt'] += $bullionDetailRow['dramount'] - $bullionDetailRow['cramount'];
}
$fine   = ($total['kg']       - $total['crfine']);
$amount = ($total['dramount'] - $total['cramount']);

echo number_format($fine, 0, '.', '').",".number_format($amount, 0, '.', '');
?>